package com.bawei.search.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.SimpleVideoEntity
import com.bawei.image.ImgOptions
import com.bawei.image.LoadImageUtil
import com.bawei.search.databinding.ItemSearchVideoBinding
import com.bumptech.glide.Glide
import kotlin.math.max

/**
 * Author: Jason Zhang
 * Date: 2025/1/17
 * Desc: 首页视频列表数据适配器
 */
class SearchVideoListAdapter constructor(private val list: MutableList<SimpleVideoEntity>) :
    RecyclerView.Adapter<SearchVideoListAdapter.ItemViewHolder>() {

    private var scrollSate = RecyclerView.SCROLL_STATE_IDLE //滚动停止
    private var isReloading = false //是否正在预加载中
    var preloadData: (()-> Unit)? = null //预加载执行方法
    var preloadEndCount = 0 //滚到倒数第几个Item时候触发预加载

    private var itemClickListener: OnItemClickListener? = null

    inner class ItemViewHolder(val dataBinding: ItemSearchVideoBinding) :
        RecyclerView.ViewHolder(dataBinding.root)

    interface OnItemClickListener {
        fun onItemClick(v: View, position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        itemClickListener = listener
    }

    override fun getItemCount(): Int = list.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        var itemDataBinding =
            ItemSearchVideoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(itemDataBinding)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        //设置标题和描述
        holder.dataBinding.tvTitle.text = entity.title
        holder.dataBinding.tvDesc.text = entity.description
        //设置作者头像
        LoadImageUtil.loadUrl(
            holder.dataBinding.root.context,
            entity.avatar_url,
            holder.dataBinding.ivAvatar,
            ImgOptions.Builder.isCircle(true).build()
        )
        //视频封面图
        LoadImageUtil.loadUrl(
            holder.dataBinding.root.context,
            entity.videomainimag,
            holder.dataBinding.ivVideoThumbnail,
            ImgOptions.Builder.isCircle(false).roundRadius(10).build()
        )
        //设置点击事件
        holder.dataBinding.ivVideoPlay.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }
        holder.dataBinding.ivCommend.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }
        holder.dataBinding.ivMore.setOnClickListener {
            itemClickListener?.onItemClick(it, position)
        }

        //检查是否执行预加载更多数据
        checkPreload(position)
    }

    /**
     * Adapter添加到RecyclerView时回调
     */
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                scrollSate = newState
                super.onScrollStateChanged(recyclerView, newState)
            }
        })
    }

    /**
     * View被回收了
     */
    override fun onViewRecycled(holder: ItemViewHolder) {
        //解除Glide和图片引用关系
        Glide.with(holder.dataBinding.root.context).clear(holder.dataBinding.ivVideoThumbnail)
        Glide.with(holder.dataBinding.root.context).clear(holder.dataBinding.ivAvatar)
    }


    /**
     * 获取Item布局类型
     */
    override fun getItemViewType(position: Int): Int {
        return 0
    }

    private fun checkPreload(position: Int){
        if (scrollSate != RecyclerView.SCROLL_STATE_IDLE //滚动中
            && position == max(list.size - 1 - preloadEndCount, 0) //倒数第几个位置
            && !isReloading //是否处于加载中
            ){
            isReloading = true
            //执行预加载方法
            preloadData?.invoke()
        } else {
            isReloading = false
        }
    }


}